﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using HNCDataSetTableAdapters;

public partial class Admin_Products_Product : System.Web.UI.Page
{
    CategoryTableAdapter cate = new CategoryTableAdapter();
    ProductsTableAdapter pro = new ProductsTableAdapter();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadProduct();  
        }
    }
    protected void gvProduct_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        pro.DeleteProductByProductID(true, Convert.ToInt32(e.CommandArgument.ToString()));
        Response.Redirect("~/Admin/Products/Product.aspx");
    }

    public void loadProduct()
    {
        List<Product> tem = new List<Product>();

        List<Category> cate = HNCData.GetAllCategoryByParentID(3);
        foreach (Category item in cate)
        {
            List<Category> cate2 = HNCData.GetAllCategoryByParentID(item.CateID).ToList<Category>();

            foreach (Category item2 in cate2)
            {
                List<Product> pro = HNCData.GetAllProductByCateID(item2.CateID);
                foreach (Product item3 in pro)
                {
                    Product obj = HNCData.GetOne(item3.ProductID);
                    if (obj != null)
                    {
                        tem.Add(obj);
                    }
                }
            }
        }

        var sapxep = (from a in tem
                      where a.Delete == false
                      orderby a.Editdate descending, a.Order ascending
                      select a);

        List<Product> list = sapxep.ToList<Product>();

        CollectionPager1.PageSize = 100;
        CollectionPager1.DataSource = list;
        CollectionPager1.BindToControl = gvProduct;
        gvProduct.DataSource = CollectionPager1.DataSourcePaged;
        gvProduct.DataBind();  
    }

}